package com.topscomm.mpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectRiskEntity;
import com.topscomm.mpm.pojo.MpmProjectRiskEntity;
import com.topscomm.mpm.service.impl.MpmProjectRiskService;
import com.topscomm.mpm.service.impl.MpmProjectRiskService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

public class MpmProjectRiskScript {
    //状态enum 用流程节点名字来同步风险开放状态
    private enum NodeName {
        Start {
            public String toString() {
                return "开始";
            }
        },
        Finish {
            public String toString() {
                return "结束";
            }
        }
    }

    private enum riskStatus {
        Open {
            public String toString() {
                return "0";
            }
        },
        Close {
            public String toString() {
                return "2";
            }
        }
    }

    /**
     * @description: 同步风险状态和审核状态
     * @author: xinfushuo
     * @param: 项目风险流程的service 和当前nodeins对应的项目风险entity
     * @throws:
     * @return:
     * @date: 2021/8/19
     * @modify:
     */
    @Transactional
    public void SyncRiskStatus(ITapService service, TapEntity entity) throws Exception {
        //这一段放在后台是因为这是放到开始和结束节点的脚本，这两个节点没有对应的审批页面，也就没有对应的前端调用方法
        CboApprovalNodeInsEntity nodeinsentity = (CboApprovalNodeInsEntity) entity;
        MpmProjectRiskService Riskservice = (MpmProjectRiskService) SpringUtil.getBean("mpmProjectRiskService");
        MpmProjectRiskEntity Riskentity = Riskservice.queryById(ConvertUtil.convertToString(nodeinsentity.getSourceid()));
        //Risk status 与nodeins字段extendfield2(在流程中默认为节点开始名字，开始节点默认为开始，结束节点默认为结束)绑定
        //若当前风险状态为审核中,且尚未走到结束节点
        if (Riskentity.getState() == 1 && !nodeinsentity.getExtendfield2().contentEquals(NodeName.Finish.toString())) {
            Riskentity.setRiskstatus(riskStatus.Open.toString());
        } else {
            Riskentity.setRiskstatus(riskStatus.Close.toString());
        }
        Riskservice.updateEntity(Riskentity);
    }

    /**
     * @description:同步项目最新项目经理信息
     * @author: xinfushuo
     * @param:项目风险流程的service 和当前nodeins对应的项目风险entity
     * @throws:
     * @return:
     * @date: 2021/8/9
     * @modify:
     */
    @Transactional
    public void syncProjectInfo(ITapService service, TapEntity entity) throws Exception {
        //获取项目entity和关联的Projec的Entity
        CboApprovalNodeInsEntity nodeInsEntity = (CboApprovalNodeInsEntity) entity;
        MpmProjectRiskService RiskService = (MpmProjectRiskService) SpringUtil.getBean("mpmProjectRiskService");
        MpmProjectRiskEntity RiskEntity = RiskService.queryById(ConvertUtil.convertToString(nodeInsEntity.getSourceid()));
        MpmProjectService projectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");
        MpmProjectEntity projectEntity = (MpmProjectEntity) projectService.queryById(ConvertUtil.convertToString(RiskEntity.getProjectid()));
        //获取当前项目经理和工号,不匹配则更新
        boolean managerChanged = !projectEntity.getProjectmanagercode().contentEquals(RiskEntity.getProjectmanagercode());
        boolean levelChanged = !projectEntity.getProjectlevel().contentEquals(RiskEntity.getProjectlevel());
        //项目经理信息变更
        if (managerChanged) {
            RiskEntity.setProjectmanager(projectEntity.getProjectmanager());
            RiskEntity.setProjectmanagercode(projectEntity.getProjectmanagercode());
        }
        //项目等级变更
        if (levelChanged) {
            RiskEntity.setRisklevel(projectEntity.getProjectlevel());
        }
        //任意一个变更则更新
        if (managerChanged || levelChanged) {
            RiskService.updateEntity(RiskEntity);

        }

    }
}
